package com.wkq.move.module.main.frame.view;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.snackbar.Snackbar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wkq.base.frame.mosby.delegate.MvpView;
import com.wkq.lib_base.adapter.KtDataBindingAdapter;
import com.wkq.lib_base.utlis.AlertUtil;
import com.wkq.lib_base.utlis.StatusBarUtil;
import com.wkq.move.R;
import com.wkq.move.module.main.ui.adapter.MoveDownAdapter;
import com.wkq.move.module.main.ui.adapter.MoveTopAdapter;
import com.wkq.move.module.main.ui.adapter.TvShowAdapter;
import com.wkq.move.module.main.ui.fragment.MovieListFragment;
import com.wkq.move.module.tv.ui.activity.UsTvShowActivity;
import com.wkq.move.utlis.DownUtil;
import com.wkq.net.model.MoveInfo;
import com.wkq.net.model.MoveListsInfo;
import com.wkq.net.model.TvListsInfo;
import com.wkq.net.model.USTVShowInfo;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.indicator.RoundLinesIndicator;
import com.youth.banner.util.BannerUtils;
import com.youth.banner.util.LogUtils;

import org.jetbrains.annotations.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 作者: 吴奎庆
 * <p>
 * 时间: 2020/5/23
 * <p>
 * 简介:
 */
public class MoveView implements MvpView {
    MovieListFragment mFragment;

    private MoveDownAdapter moveDownAdapter;


    public MoveView(MovieListFragment fragment) {
        mFragment = fragment;
    }

    public void initView() {
        mFragment.binding.appbarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (!mFragment.isExpend()) {
                        StatusBarUtil.setDarkMode(mFragment.getActivity());
                        mFragment.setExpend(!mFragment.isExpend());

                        mFragment.binding.toolbar.setVisibility(View.VISIBLE);
                        mFragment.binding.tvTitle.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFragment.isExpend()) {
                        StatusBarUtil.setDarkMode(mFragment.getActivity());
                        mFragment.setExpend(!mFragment.isExpend());
                        mFragment.binding.toolbar.setVisibility(View.GONE);

                        mFragment.binding.tvTitle.setVisibility(View.GONE);
                    }
                }
            }
        });



        moveDownAdapter = new MoveDownAdapter(mFragment.getActivity(), R.layout.item_move_down);
        mFragment.binding.rvMovies.setLayoutManager(new LinearLayoutManager(mFragment.getActivity()));
        mFragment.binding.rvMovies.setAdapter(moveDownAdapter);

        mFragment.binding.rvSf.setEnableRefresh(false);
        mFragment.binding.bannerMovies.addBannerLifecycleObserver(mFragment)//添加生命周期观察者
                .setBannerRound(BannerUtils.dp2px(5))//圆角
//              .addPageTransformer(new RotateYTransformer())//添加切换效果
                .setIndicator(new CircleIndicator(mFragment.getActivity()))//设置指示器
                .addOnPageChangeListener(mFragment);//添加切换监听

        mFragment.binding.rvSf.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (!TextUtils.isEmpty(getNexPage(mFragment.getIndex()))) {
                    mFragment.getPresenter().getData(getNexPage(mFragment.getIndex()));
                } else {
                    mFragment.binding.rvSf.finishRefresh();
                    mFragment.binding.rvSf.finishLoadMore();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mFragment.getPresenter().getData(mFragment.getStartIndex());
            }
        });


    }

    boolean isInitBanner;

    public void showInfos(MoveListsInfo infos) {
        mFragment.setTotalIndex(infos.getTotalIndex());
        if (!TextUtils.isEmpty(getNexPage(mFragment.getIndex())))
            mFragment.setIndex(getNexPage(mFragment.getIndex()));
        if (!isInitBanner) initBanner(infos);
        if (infos.getMoveList() != null) moveDownAdapter.addItems(infos.getMoveList());
        mFragment.binding.rvSf.finishRefresh();
        mFragment.binding.rvSf.finishLoadMore();
    }


    private void initBanner(MoveListsInfo infos) {
        isInitBanner = true;
        mFragment.binding.bannerMovies.setAdapter(new MoveTopAdapter(infos.getTvList()));
        mFragment.binding.bannerMovies.setIndicator(new RoundLinesIndicator(mFragment.getActivity()));
        mFragment.binding.bannerMovies.setIndicatorSelectedWidth((int) BannerUtils.dp2px(15));

        mFragment.binding.bannerMovies.setOnBannerListener((data, position) -> {
//            Snackbar.make(mFragment.binding.bannerMovies, ((USTVShowInfo) data).getTvName(), Snackbar.LENGTH_SHORT).show();
            if(!TextUtils.isEmpty(((USTVShowInfo) data).getTvSite()))
            DownUtil.INSTANCE.openXunLei(mFragment.getActivity(),((USTVShowInfo) data).getTvSite());
        });
    }


    public boolean hasNext(String currentINdex) {
        Pattern p = Pattern.compile("\\d+");
        Pattern p2 = Pattern.compile("\\d+");

        Matcher mToTal = p.matcher(mFragment.getTotalIndex());
        Matcher index = p2.matcher(currentINdex);
        mToTal.find();
        index.find();

        return Integer.parseInt(index.group()) <= Integer.parseInt(mToTal.group());
    }


    public String getNexPage(String nowIndex) {
        if (hasNext(nowIndex)) {
            Pattern p = Pattern.compile("\\d+");
            Matcher nowIndexM = p.matcher(nowIndex);
            nowIndexM.find();

            int page = Integer.parseInt(nowIndexM.group());
            int newPage = (page + 1);
            return nowIndex.replace(page + "", newPage + "");
        }
        return nowIndex;
    }




    public void showMessage(@Nullable String message) {
        if (mFragment == null || mFragment.getActivity() == null || TextUtils.isEmpty(message))
            return;

        mFragment.binding.rvSf.finishRefresh();
        mFragment.binding.rvSf.finishLoadMore();
        AlertUtil.showDeftToast(mFragment.getActivity(), message);
    }
}
